//
//  PersonalHomeViewController.m
//  TheRoadAD
//
//  Created by baixue on 16/8/15.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "PersonalHomeViewController.h"
#import "PersonalCell.h"
#import "MyHeaderView.h"
#import "ComTableView.h"//底部的tableView
#import "ComDetailsViewController.H"
#import "DynamicCell.h"
#import "DynamicModel.h"
#import "FooterView.h"
#define titleBtn @[@"全部",@"活动",@"改装"]

static NSString *const PERSONAL = @"DynamicCell";

@interface PersonalHomeViewController ()<UIScrollViewDelegate,UITableViewDataSource,UITableViewDelegate>
{
    UIView *_line;
    UIScrollView *_scrollerView;
}
@property (nonatomic, strong) NSMutableArray *modelsArray;
@property (strong, nonatomic) MyHeaderView *myHeaderView;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation PersonalHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpHeader];
    [self setTableViewWithCount:0];
    [self setTableViewWithCount:1];
    [self setTableViewWithCount:2];
//    [self setTableView];
//    [self setactity];
//    [self setmodified];
    [self creatModelsWithCount:10];
    
   
    
}

//添加headerView
-(void)setUpHeader{
    MyHeaderView *headerView = [[MyHeaderView alloc] initWithFrame:CGRectMake(0, 64, Screen_Width, Screen_Height)];
    _line = headerView.line;
    _scrollerView = headerView.scrollerView;
    _scrollerView.delegate = self;
    [self.view addSubview:headerView];
    headerView.SelectBlock = ^(NSInteger tag){
        [UIView animateWithDuration:0.25 animations:^{
            
            _line.frame = CGRectMake(Screen_Width/3*(tag-125)+30, 45, Screen_Width/3-60, 3);
            _scrollerView.contentOffset = CGPointMake(Screen_Width*(tag-125), 0);
        }];
    };
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollerView.contentOffset.x/Screen_Width;
    [UIView animateWithDuration:0.25 animations:^{
        _line.frame = CGRectMake(Screen_Width/3*index +30, 45, Screen_Width/3-60, 3);
    }];
}



- (void)setTableViewWithCount:(NSInteger)count{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(Screen_Width * count, 0, Screen_Width, Screen_Height-49-63) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_scrollerView addSubview:_tableView];
    
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //注册
    [_tableView registerNib:[UINib nibWithNibName:@"DynamicCell" bundle:nil] forCellReuseIdentifier:PERSONAL];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.modelsArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DynamicCell *cell = [tableView dequeueReusableCellWithIdentifier:PERSONAL];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"DynamicCell" owner:nil options:nil] lastObject];
    }
    
    cell.model = self.modelsArray[indexPath.section];
    
    ////// 此步设置用于实现cell的frame缓存，可以让tableview滑动更加流畅 //////
    
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    


    
//    [cell cellAutoLayoutHeight:cell.content.text];

    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 0.01f;
    }
    return 8.0f;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    FooterView *footerView = [[[NSBundle mainBundle]loadNibNamed:@"FooterView" owner:nil options:nil]lastObject];
    
    return footerView;
}

//返回cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [self.tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.section] keyPath:@"model" cellClass:[DynamicCell class] contentViewWidth:Screen_Width];
}

//模拟假数据
- (void)creatModelsWithCount:(NSInteger)count
{
    if (!_modelsArray) {
        _modelsArray = [NSMutableArray new];
    }
    
    NSArray *iconImageNamesArray = @[@"icon0.jpg",
                                     @"icon1.jpg",
                                     @"icon2.jpg",
                                     @"icon3.jpg",
                                     @"icon4.jpg",
                                     ];
    
    NSArray *namesArray = @[@"GSD_iOS",
                            @"风口上的猪",
                            @"当今世界网名都不好起了",
                            @"我叫郭德纲",
                            @"Hello Kitty"];
    
    NSArray *timer = @[@"2016-09-12 16:45",
                            @"2016-09-12 16:45",
                            @"2016-09-12 16:45",
                            @"2016-09-12 16:45",
                            @"2016-09-12 16:45"];
    
    NSArray *textArray = @[@"当你的 app 没有提供 3x 的 LaunchImage 时，系统默认进入兼容模式，大屏幕一切按照 320 宽度渲染，屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。",
                           @"然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。",
                           @"当你的 app 没有提供 3x 的 LaunchImage 时",
                           @"但是建议不要长期处于这种模式下，否则在大屏上会显得字大，内容少，容易遭到用户投诉。",
                           @"屏幕宽度返回 320；然后等比例拉伸到大屏。这种情况下对界面不会产生任何影响，等于把小屏完全拉伸。但是建议不要长期处于这种模式下。"
                           ];
    
    NSArray *picImageNamesArray = @[@"icon0.jpg",
                                    @"icon1.jpg",
                                    @"icon2.jpg",
                                    @"icon3.jpg",
                                    @"icon4.jpg",
                                    ];
    
    for (int i = 0; i < count; i++) {
        int iconRandomIndex = arc4random_uniform(5);
        int nameRandomIndex = arc4random_uniform(5);
        int contentRandomIndex = arc4random_uniform(5);
        int picRandomIndex = arc4random_uniform(5);
        
        DynamicModel *model = [DynamicModel new];
        model.headImg = iconImageNamesArray[iconRandomIndex];
        model.userName = namesArray[nameRandomIndex];
        model.content = textArray[contentRandomIndex];
        model.timer = timer[contentRandomIndex];
        
        
        // 模拟“有或者无图片”
        int random = arc4random_uniform(100);
        if (random <= 0) {
            model.pictureImg = picImageNamesArray[picRandomIndex];
        }
        
        [self.modelsArray addObject:model];
    }
}



@end
